
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言循环语句 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part11/" />
    
    
    <link rel="prev" href="../part9/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言循环语句</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x5FAA;&#x73AF;&#x8BED;&#x53E5;">Go &#x8BED;&#x8A00;&#x5FAA;&#x73AF;&#x8BED;&#x53E5;</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x5728;&#x4E0D;&#x5C11;&#x5B9E;&#x9645;&#x95EE;&#x9898;&#x4E2D;&#x6709;&#x8BB8;&#x591A;&#x5177;&#x6709;&#x89C4;&#x5F8B;&#x6027;&#x7684;&#x91CD;&#x590D;&#x64CD;&#x4F5C;&#xFF0C;&#x56E0;&#x6B64;&#x5728;&#x7A0B;&#x5E8F;&#x4E2D;&#x5C31;&#x9700;&#x8981;&#x91CD;&#x590D;&#x6267;&#x884C;&#x67D0;&#x4E9B;&#x8BED;&#x53E5;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x4E3A;&#x5927;&#x591A;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x5FAA;&#x73AF;&#x7A0B;&#x5E8F;&#x7684;&#x6D41;&#x7A0B;&#x56FE;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part10/01.png">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;">&#x5FAA;&#x73AF;&#x7A0B;&#x5E8F;&#x6D41;&#x7A0B;&#x56FE;</div>
</center>

<p>Go &#x8BED;&#x8A00;&#x63D0;&#x4F9B;&#x4E86;&#x4EE5;&#x4E0B;&#x51E0;&#x79CD;&#x7C7B;&#x578B;&#x5FAA;&#x73AF;&#x5904;&#x7406;&#x8BED;&#x53E5;&#xFF1A;</p>
<table>
    <tr>
        <th bgcolor="#4b4b4b"><font color="#ffffff">&#x5FAA;&#x73AF;&#x7C7B;&#x578B;</font></th>
        <th bgcolor="#4b4b4b" width="100%"><font color="#ffffff">&#x63CF;&#x8FF0;</font></th>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">for &#x5FAA;&#x73AF;</td>
        <td align="center" bgcolor="#f5f2ee">&#x91CD;&#x590D;&#x6267;&#x884C;&#x8BED;&#x53E5;&#x5757;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#ffffff">&#x5FAA;&#x73AF;&#x5D4C;&#x5957;</td>
        <td align="center" bgcolor="#ffffff">&#x5728; for &#x5FAA;&#x73AF;&#x4E2D;&#x5D4C;&#x5957;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A; for &#x5FAA;&#x73AF;</td>
    </tr>
</table>

<h3 id="for-&#x5FAA;&#x73AF;">for &#x5FAA;&#x73AF;</h3>
<p>for &#x5FAA;&#x73AF;&#x662F;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#x63A7;&#x5236;&#x7ED3;&#x6784;&#xFF0C;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x6307;&#x5B9A;&#x6B21;&#x6570;&#x7684;&#x5FAA;&#x73AF;&#x3002;</p>
<h4 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h4>
<p>Go &#x8BED;&#x8A00;&#x7684; For &#x5FAA;&#x73AF;&#x6709; 3 &#x79CD;&#x5F62;&#x5F0F;&#xFF0C;&#x53EA;&#x6709;&#x5176;&#x4E2D;&#x7684;&#x4E00;&#x79CD;&#x4F7F;&#x7528;&#x5206;&#x53F7;&#x3002;</p>
<p>&#x548C; C &#x8BED;&#x8A00;&#x7684; <code>for</code> &#x4E00;&#x6837;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">for</span> init; condition; post { }
</code></pre>
<p>&#x548C; C &#x7684; <code>while</code> &#x4E00;&#x6837;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">for</span> condition { }
</code></pre>
<p>&#x548C; C &#x7684; <code>for(;;)</code> &#x4E00;&#x6837;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">for</span> { }
</code></pre>
<ul>
<li><code>init</code>&#xFF1A; &#x4E00;&#x822C;&#x4E3A;&#x8D4B;&#x503C;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x7ED9;&#x63A7;&#x5236;&#x53D8;&#x91CF;&#x8D4B;&#x521D;&#x503C;&#xFF1B;</li>
<li><code>condition</code>&#xFF1A; &#x5173;&#x7CFB;&#x8868;&#x8FBE;&#x5F0F;&#x6216;&#x903B;&#x8F91;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x5FAA;&#x73AF;&#x63A7;&#x5236;&#x6761;&#x4EF6;&#xFF1B;</li>
<li><code>post</code>&#xFF1A; &#x4E00;&#x822C;&#x4E3A;&#x8D4B;&#x503C;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x7ED9;&#x63A7;&#x5236;&#x53D8;&#x91CF;&#x589E;&#x91CF;&#x6216;&#x51CF;&#x91CF;&#x3002;</li>
</ul>
<p>for&#x8BED;&#x53E5;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x5982;&#x4E0B;&#xFF1A;</p>
<ul>
<li>&#x5148;&#x5BF9;&#x8868;&#x8FBE;&#x5F0F; 1 &#x8D4B;&#x521D;&#x503C;&#xFF1B;</li>
<li>&#x5224;&#x522B;&#x8D4B;&#x503C;&#x8868;&#x8FBE;&#x5F0F; init &#x662F;&#x5426;&#x6EE1;&#x8DB3;&#x7ED9;&#x5B9A;&#x6761;&#x4EF6;&#xFF0C;&#x82E5;&#x5176;&#x503C;&#x4E3A;&#x771F;&#xFF0C;&#x6EE1;&#x8DB3;&#x5FAA;&#x73AF;&#x6761;&#x4EF6;&#xFF0C;&#x5219;&#x6267;&#x884C;&#x5FAA;&#x73AF;&#x4F53;&#x5185;&#x8BED;&#x53E5;&#xFF0C;&#x7136;&#x540E;&#x6267;&#x884C; post&#xFF0C;&#x8FDB;&#x5165;&#x7B2C;&#x4E8C;&#x6B21;&#x5FAA;&#x73AF;&#xFF0C;&#x518D;&#x5224;&#x522B; condition&#xFF1B;&#x5426;&#x5219;&#x5224;&#x65AD; condition &#x7684;&#x503C;&#x4E3A;&#x5047;&#xFF0C;&#x4E0D;&#x6EE1;&#x8DB3;&#x6761;&#x4EF6;&#xFF0C;&#x5C31;&#x7EC8;&#x6B62;for&#x5FAA;&#x73AF;&#xFF0C;&#x6267;&#x884C;&#x5FAA;&#x73AF;&#x4F53;&#x5916;&#x8BED;&#x53E5;&#x3002;</li>
</ul>
<p>for &#x5FAA;&#x73AF;&#x7684; range &#x683C;&#x5F0F;&#x53EF;&#x4EE5;&#x5BF9; slice&#x3001;map&#x3001;&#x6570;&#x7EC4;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x7B49;&#x8FDB;&#x884C;&#x8FED;&#x4EE3;&#x5FAA;&#x73AF;&#x3002;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">for</span> key, value := <span class="hljs-keyword">range</span> oldMap {
    newMap[key] = value
}
</code></pre>
<p>for &#x8BED;&#x53E5;&#x8BED;&#x6CD5;&#x6D41;&#x7A0B;&#x5982;&#x4E0B;&#x56FE;&#x6240;&#x793A;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part10/02.jpg">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;">for &#x8BED;&#x53E5;&#x8BED;&#x6CD5;&#x6D41;&#x7A0B;&#x56FE;</div>
</center>

<h4 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h4>
<p>&#x8BA1;&#x7B97; 1 &#x5230; 10 &#x7684;&#x6570;&#x5B57;&#x4E4B;&#x548C;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    tmpSum := <span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> i := <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">11</span>; i++ {
        tmpSum += i
    }
    fmt.Println(tmpSum) <span class="hljs-comment">// 55</span>
}
</code></pre>
<p>init &#x548C; post &#x53C2;&#x6570;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x7701;&#x7565;&#x5B83;&#xFF0C;&#x7C7B;&#x4F3C; While &#x8BED;&#x53E5;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x5728; sum &#x5C0F;&#x4E8E; 10 &#x7684;&#x65F6;&#x5019;&#x8BA1;&#x7B97; sum &#x81EA;&#x76F8;&#x52A0;&#x540E;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    sum := <span class="hljs-number">1</span>
    <span class="hljs-keyword">for</span> ; sum &lt;= <span class="hljs-number">10</span>; {
        sum += sum
    }
    fmt.Println(sum)    <span class="hljs-comment">// 16</span>

    <span class="hljs-comment">// &#x8FD9;&#x6837;&#x5199;&#x4E5F;&#x53EF;&#x4EE5;&#xFF0C;&#x66F4;&#x50CF; While &#x8BED;&#x53E5;&#x5F62;&#x5F0F;</span>
    sum = <span class="hljs-number">1</span>
    <span class="hljs-keyword">for</span> sum &lt;= <span class="hljs-number">10</span> {
        sum += sum
    }
    fmt.Println(sum)    <span class="hljs-comment">// 16</span>
}
</code></pre>
<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x65E0;&#x9650;&#x5FAA;&#x73AF;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    sum := <span class="hljs-number">0</span>
    <span class="hljs-keyword">for</span> {
        sum++ <span class="hljs-comment">// &#x65E0;&#x9650;&#x5FAA;&#x73AF;&#x4E0B;ss&#x53BB;</span>
    }
    fmt.Println(sum) <span class="hljs-comment">// &#x65E0;&#x6CD5;&#x8F93;&#x51FA;</span>
}
</code></pre>
<p>&#x8981;&#x505C;&#x6B62;&#x65E0;&#x9650;&#x5FAA;&#x73AF;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x547D;&#x4EE4;&#x7A97;&#x53E3;&#x6309;&#x4E0B;<code>ctrl-c</code> &#x3002;</p>
<h5 id="for-each-range-&#x5FAA;&#x73AF;">For-each range &#x5FAA;&#x73AF;</h5>
<p>&#x8FD9;&#x79CD;&#x683C;&#x5F0F;&#x7684;&#x5FAA;&#x73AF;&#x53EF;&#x4EE5;&#x5BF9;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x6570;&#x7EC4;&#x3001;&#x5207;&#x7247;&#x7B49;&#x8FDB;&#x884C;&#x8FED;&#x4EE3;&#x8F93;&#x51FA;&#x5143;&#x7D20;&#x3002;
&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    strings := [] <span class="hljs-keyword">string</span> {<span class="hljs-string">&quot;google&quot;</span>, <span class="hljs-string">&quot;apple&quot;</span>}
    <span class="hljs-keyword">for</span> i, s := <span class="hljs-keyword">range</span> strings {
        fmt.Println(i, s)
    }

    numbers := [] <span class="hljs-keyword">int</span> {<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>}
    <span class="hljs-keyword">for</span> i, x := <span class="hljs-keyword">range</span> numbers {
        fmt.Printf(<span class="hljs-string">&quot;&#x7B2C; %d &#x4F4D; x &#x7684;&#x503C;&#x662F; %d\n&quot;</span>, i, x)
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;:</p>
<pre><code class="lang-sh">0 google
1 apple
&#x7B2C; 0 &#x4F4D; x &#x7684;&#x503C;&#x662F; 1
&#x7B2C; 1 &#x4F4D; x &#x7684;&#x503C;&#x662F; 3
&#x7B2C; 2 &#x4F4D; x &#x7684;&#x503C;&#x662F; 4
&#x7B2C; 3 &#x4F4D; x &#x7684;&#x503C;&#x662F; 6
</code></pre>
<h3 id="&#x5FAA;&#x73AF;&#x5D4C;&#x5957;">&#x5FAA;&#x73AF;&#x5D4C;&#x5957;</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x5141;&#x8BB8;&#x7528;&#x6237;&#x5728;&#x5FAA;&#x73AF;&#x5185;&#x4F7F;&#x7528;&#x5FAA;&#x73AF;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x5C06;&#x4E3A;&#x5927;&#x5BB6;&#x4ECB;&#x7ECD;&#x5D4C;&#x5957;&#x5FAA;&#x73AF;&#x7684;&#x4F7F;&#x7528;&#x3002;</p>
<h4 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h4>
<p>&#x4EE5;&#x4E0B;&#x4E3A; Go &#x8BED;&#x8A00;&#x5D4C;&#x5957;&#x5FAA;&#x73AF;&#x7684;&#x683C;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">for</span> [condition |  ( init; condition; increment ) | Range] {
    <span class="hljs-keyword">for</span> [condition |  ( init; condition; increment ) | Range] {
        statement(s)
    }
    statement(s)
}
</code></pre>
<h4 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h4>
<p>&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x4F7F;&#x7528;&#x5FAA;&#x73AF;&#x5D4C;&#x5957;&#x6765;&#x8F93;&#x51FA; 2 &#x5230; 100 &#x95F4;&#x7684;&#x7D20;&#x6570;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> i, j <span class="hljs-keyword">int</span>
    <span class="hljs-keyword">for</span> i = <span class="hljs-number">2</span>; i &lt; <span class="hljs-number">100</span>; i ++ {
        <span class="hljs-keyword">for</span> j = <span class="hljs-number">2</span>; j &lt;= (i / j); j++ {
            <span class="hljs-keyword">if</span> i % j == <span class="hljs-number">0</span> {
                <span class="hljs-keyword">break</span>    <span class="hljs-comment">// &#x5982;&#x679C;&#x53D1;&#x73B0;&#x56E0;&#x5B50;&#xFF0C;&#x5219;&#x4E0D;&#x662F;&#x7D20;&#x6570;</span>
            }
        }
        <span class="hljs-keyword">if</span> j &gt; (i / j) {
            fmt.Printf(<span class="hljs-string">&quot;%d &#x662F;&#x7D20;&#x6570;\n&quot;</span>, i)
        }
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;:</p>
<pre><code class="lang-sh">2 &#x662F;&#x7D20;&#x6570;
3 &#x662F;&#x7D20;&#x6570;
5 &#x662F;&#x7D20;&#x6570;
7 &#x662F;&#x7D20;&#x6570;
11 &#x662F;&#x7D20;&#x6570;
13 &#x662F;&#x7D20;&#x6570;
17 &#x662F;&#x7D20;&#x6570;
19 &#x662F;&#x7D20;&#x6570;
23 &#x662F;&#x7D20;&#x6570;
29 &#x662F;&#x7D20;&#x6570;
31 &#x662F;&#x7D20;&#x6570;
37 &#x662F;&#x7D20;&#x6570;
41 &#x662F;&#x7D20;&#x6570;
43 &#x662F;&#x7D20;&#x6570;
47 &#x662F;&#x7D20;&#x6570;
53 &#x662F;&#x7D20;&#x6570;
59 &#x662F;&#x7D20;&#x6570;
61 &#x662F;&#x7D20;&#x6570;
67 &#x662F;&#x7D20;&#x6570;
71 &#x662F;&#x7D20;&#x6570;
73 &#x662F;&#x7D20;&#x6570;
79 &#x662F;&#x7D20;&#x6570;
83 &#x662F;&#x7D20;&#x6570;
89 &#x662F;&#x7D20;&#x6570;
97 &#x662F;&#x7D20;&#x6570;
</code></pre>
<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x4E5D;&#x4E5D;&#x4E58;&#x6CD5;&#x8868;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">for</span> i := <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">10</span>; i++ {
        <span class="hljs-keyword">for</span> j := <span class="hljs-number">1</span>; j &lt;= i; j++ {
            fmt.Printf(<span class="hljs-string">&quot;%d x %d = %d  &quot;</span>, j, i, j * i)
        }
        fmt.Println(<span class="hljs-string">&quot;&quot;</span>)
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;:</p>
<pre><code class="lang-sh">1 x 1 = 1  
1 x 2 = 2  2 x 2 = 4  
1 x 3 = 3  2 x 3 = 6  3 x 3 = 9  
1 x 4 = 4  2 x 4 = 8  3 x 4 = 12  4 x 4 = 16  
1 x 5 = 5  2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25  
1 x 6 = 6  2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36  
1 x 7 = 7  2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49  
1 x 8 = 8  2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64  
1 x 9 = 9  2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81
</code></pre>
<h2 id="&#x5FAA;&#x73AF;&#x63A7;&#x5236;&#x8BED;&#x53E5;">&#x5FAA;&#x73AF;&#x63A7;&#x5236;&#x8BED;&#x53E5;</h2>
<p>&#x5FAA;&#x73AF;&#x63A7;&#x5236;&#x8BED;&#x53E5;&#x53EF;&#x4EE5;&#x63A7;&#x5236;&#x5FAA;&#x73AF;&#x4F53;&#x5185;&#x8BED;&#x53E5;&#x7684;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x3002;</p>
<p>GO &#x8BED;&#x8A00;&#x652F;&#x6301;&#x4EE5;&#x4E0B;&#x51E0;&#x79CD;&#x5FAA;&#x73AF;&#x63A7;&#x5236;&#x8BED;&#x53E5;&#xFF1A;</p>
<table>
    <tr>
        <th bgcolor="#4b4b4b"><font color="#ffffff">&#x63A7;&#x5236;&#x8BED;&#x53E5;</font></th>
        <th bgcolor="#4b4b4b" width="100%"><font color="#ffffff">&#x63CF;&#x8FF0;</font></th>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">break &#x8BED;&#x53E5;</td>
        <td align="center" bgcolor="#f5f2ee">&#x7ECF;&#x5E38;&#x7528;&#x4E8E;&#x4E2D;&#x65AD;&#x5F53;&#x524D; for &#x5FAA;&#x73AF;&#x6216;&#x8DF3;&#x51FA; switch &#x8BED;&#x53E5;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#ffffff">continue &#x8BED;&#x53E5;</td>
        <td align="center" bgcolor="#ffffff">&#x8DF3;&#x8FC7;&#x5F53;&#x524D;&#x5FAA;&#x73AF;&#x7684;&#x5269;&#x4F59;&#x8BED;&#x53E5;&#xFF0C;&#x7136;&#x540E;&#x7EE7;&#x7EED;&#x8FDB;&#x884C;&#x4E0B;&#x4E00;&#x8F6E;&#x5FAA;&#x73AF;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">goto &#x8BED;&#x53E5;</td>
        <td align="center" bgcolor="#f5f2ee">&#x5C06;&#x63A7;&#x5236;&#x8F6C;&#x79FB;&#x5230;&#x88AB;&#x6807;&#x8BB0;&#x7684;&#x8BED;&#x53E5;</td>
    </tr>
</table>


<h3 id="break-&#x8BED;&#x53E5;">break &#x8BED;&#x53E5;</h3>
<p>Go &#x8BED;&#x8A00;&#x4E2D; break &#x8BED;&#x53E5;&#x7528;&#x4E8E;&#x4EE5;&#x4E0B;&#x4E24;&#x65B9;&#x9762;&#xFF1A;</p>
<ul>
<li>&#x7528;&#x4E8E;&#x5FAA;&#x73AF;&#x8BED;&#x53E5;&#x4E2D;&#x8DF3;&#x51FA;&#x5FAA;&#x73AF;&#xFF0C;&#x5E76;&#x5F00;&#x59CB;&#x6267;&#x884C;&#x5FAA;&#x73AF;&#x4E4B;&#x540E;&#x7684;&#x8BED;&#x53E5;&#xFF1B;</li>
<li>break &#x5728; switch&#xFF08;&#x5F00;&#x5173;&#x8BED;&#x53E5;&#xFF09;&#x4E2D;&#x5728;&#x6267;&#x884C;&#x4E00;&#x6761; case &#x540E;&#x8DF3;&#x51FA;&#x8BED;&#x53E5;&#x7684;&#x4F5C;&#x7528;&#xFF1B;</li>
<li>&#x5728;&#x591A;&#x91CD;&#x5FAA;&#x73AF;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6807;&#x53F7; label &#x6807;&#x51FA;&#x60F3; break &#x7684;&#x5FAA;&#x73AF;&#x3002;</li>
</ul>
<h4 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h4>
<pre><code class="lang-go"><span class="hljs-keyword">break</span>
</code></pre>
<p>break &#x8BED;&#x53E5;&#x6D41;&#x7A0B;&#x56FE;&#x5982;&#x4E0B;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part10/03.png">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;">for &#x8BED;&#x53E5;&#x8BED;&#x6CD5;&#x6D41;&#x7A0B;&#x56FE;</div>
</center>

<h4 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h4>
<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x5728;&#x53D8;&#x91CF; a &#x5927;&#x4E8E; 15 &#x7684;&#x65F6;&#x5019;&#x8DF3;&#x51FA;&#x5FAA;&#x73AF;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
   <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
   <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">10</span>

   <span class="hljs-comment">/* for &#x5FAA;&#x73AF; */</span>
   <span class="hljs-keyword">for</span> a &lt; <span class="hljs-number">20</span> {
      fmt.Printf(<span class="hljs-string">&quot;a &#x7684;&#x503C;&#x4E3A; : %d\n&quot;</span>, a);
      a++;
      <span class="hljs-keyword">if</span> a &gt; <span class="hljs-number">15</span> {
         <span class="hljs-comment">/* &#x4F7F;&#x7528; break &#x8BED;&#x53E5;&#x8DF3;&#x51FA;&#x5FAA;&#x73AF; */</span>
         <span class="hljs-keyword">break</span>;
      }
   }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a &#x7684;&#x503C;&#x4E3A; : 10
a &#x7684;&#x503C;&#x4E3A; : 11
a &#x7684;&#x503C;&#x4E3A; : 12
a &#x7684;&#x503C;&#x4E3A; : 13
a &#x7684;&#x503C;&#x4E3A; : 14
a &#x7684;&#x503C;&#x4E3A; : 15
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x6709;&#x591A;&#x91CD;&#x5FAA;&#x73AF;&#xFF0C;&#x6F14;&#x793A;&#x4E86;&#x4F7F;&#x7528;&#x6807;&#x8BB0;&#x548C;&#x4E0D;&#x4F7F;&#x7528;&#x6807;&#x8BB0;&#x7684;&#x533A;&#x522B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">// &#x4E0D;&#x4F7F;&#x7528;&#x6807;&#x8BB0;</span>
    fmt.Println(<span class="hljs-string">&quot;---- break ----&quot;</span>)
    <span class="hljs-keyword">for</span> i := <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">3</span>; i++ {
        fmt.Printf(<span class="hljs-string">&quot;i: %d\n&quot;</span>, i)
            <span class="hljs-keyword">for</span> i2 := <span class="hljs-number">11</span>; i2 &lt;= <span class="hljs-number">13</span>; i2++ {
                    fmt.Printf(<span class="hljs-string">&quot;i2: %d\n&quot;</span>, i2)
                    <span class="hljs-keyword">break</span>
            }
        }

    <span class="hljs-comment">// &#x4F7F;&#x7528;&#x6807;&#x8BB0;</span>
    fmt.Println(<span class="hljs-string">&quot;---- break label ----&quot;</span>)
    re:
        <span class="hljs-keyword">for</span> i := <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">3</span>; i++ {
            fmt.Printf(<span class="hljs-string">&quot;i: %d\n&quot;</span>, i)
            <span class="hljs-keyword">for</span> i2 := <span class="hljs-number">11</span>; i2 &lt;= <span class="hljs-number">13</span>; i2++ {
                fmt.Printf(<span class="hljs-string">&quot;i2: %d\n&quot;</span>, i2)
                <span class="hljs-keyword">break</span> re
            }
        }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">---- <span class="hljs-built_in">break</span> ----
i: 1
i2: 11
i: 2
i2: 11
i: 3
i2: 11
---- <span class="hljs-built_in">break</span> label ----
i: 1
i2: 11
</code></pre>
<h3 id="continue-&#x8BED;&#x53E5;">continue &#x8BED;&#x53E5;</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x7684; continue &#x8BED;&#x53E5; &#x6709;&#x70B9;&#x50CF; break &#x8BED;&#x53E5;&#x3002;&#x4F46;&#x662F; continue &#x4E0D;&#x662F;&#x8DF3;&#x51FA;&#x5FAA;&#x73AF;&#xFF0C;&#x800C;&#x662F;&#x8DF3;&#x8FC7;&#x5F53;&#x524D;&#x5FAA;&#x73AF;&#x6267;&#x884C;&#x4E0B;&#x4E00;&#x6B21;&#x5FAA;&#x73AF;&#x8BED;&#x53E5;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;for &#x5FAA;&#x73AF;&#x4E2D;&#xFF0C;&#x6267;&#x884C; continue &#x8BED;&#x53E5;&#x4F1A;&#x89E6;&#x53D1; for &#x589E;&#x91CF;&#x8BED;&#x53E5;&#x7684;&#x6267;&#x884C;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x5728;&#x591A;&#x91CD;&#x5FAA;&#x73AF;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6807;&#x53F7; label &#x6807;&#x51FA;&#x60F3; continue &#x7684;&#x5FAA;&#x73AF;&#x3002;</p>
<h4 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h4>
<p>continue &#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">continue</span>
</code></pre>
<p>continue &#x8BED;&#x53E5;&#x6D41;&#x7A0B;&#x56FE;&#x5982;&#x4E0B;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part10/04.png">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;">continue &#x8BED;&#x53E5;&#x8BED;&#x6CD5;&#x6D41;&#x7A0B;&#x56FE;</div>
</center>

<h4 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h4>
<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x5728;&#x53D8;&#x91CF; a &#x7B49;&#x4E8E; 15 &#x7684;&#x65F6;&#x5019;&#x8DF3;&#x8FC7;&#x672C;&#x6B21;&#x5FAA;&#x73AF;&#x6267;&#x884C;&#x4E0B;&#x4E00;&#x6B21;&#x5FAA;&#x73AF;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">10</span>

    <span class="hljs-comment">/* for &#x5FAA;&#x73AF; */</span>
    <span class="hljs-keyword">for</span> a &lt; <span class="hljs-number">20</span> {
        <span class="hljs-keyword">if</span> a == <span class="hljs-number">15</span> {
            <span class="hljs-comment">/* &#x8DF3;&#x8FC7;&#x6B64;&#x6B21;&#x5FAA;&#x73AF; */</span>
            a = a + <span class="hljs-number">1</span>
            <span class="hljs-keyword">continue</span>
        }
        fmt.Printf(<span class="hljs-string">&quot;a &#x7684;&#x503C;&#x4E3A; : %d\n&quot;</span>, a)
        a++
    }  
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a &#x7684;&#x503C;&#x4E3A; : 10
a &#x7684;&#x503C;&#x4E3A; : 11
a &#x7684;&#x503C;&#x4E3A; : 12
a &#x7684;&#x503C;&#x4E3A; : 13
a &#x7684;&#x503C;&#x4E3A; : 14
a &#x7684;&#x503C;&#x4E3A; : 16
a &#x7684;&#x503C;&#x4E3A; : 17
a &#x7684;&#x503C;&#x4E3A; : 18
a &#x7684;&#x503C;&#x4E3A; : 19
</code></pre>
<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x6709;&#x591A;&#x91CD;&#x5FAA;&#x73AF;&#xFF0C;&#x6F14;&#x793A;&#x4E86;&#x4F7F;&#x7528;&#x6807;&#x8BB0;&#x548C;&#x4E0D;&#x4F7F;&#x7528;&#x6807;&#x8BB0;&#x7684;&#x533A;&#x522B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">// &#x4E0D;&#x4F7F;&#x7528;&#x6807;&#x8BB0;</span>
    fmt.Println(<span class="hljs-string">&quot;---- continue ---- &quot;</span>)
    <span class="hljs-keyword">for</span> i := <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">3</span>; i++ {
        fmt.Printf(<span class="hljs-string">&quot;i: %d\n&quot;</span>, i)
            <span class="hljs-keyword">for</span> i2 := <span class="hljs-number">11</span>; i2 &lt;= <span class="hljs-number">13</span>; i2++ {
                fmt.Printf(<span class="hljs-string">&quot;i2: %d\n&quot;</span>, i2)
                <span class="hljs-keyword">continue</span>
            }
    }

    <span class="hljs-comment">// &#x4F7F;&#x7528;&#x6807;&#x8BB0;</span>
    fmt.Println(<span class="hljs-string">&quot;---- continue label ----&quot;</span>)
    re:
        <span class="hljs-keyword">for</span> i := <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">3</span>; i++ {
            fmt.Printf(<span class="hljs-string">&quot;i: %d\n&quot;</span>, i)
                <span class="hljs-keyword">for</span> i2 := <span class="hljs-number">11</span>; i2 &lt;= <span class="hljs-number">13</span>; i2++ {
                    fmt.Printf(<span class="hljs-string">&quot;i2: %d\n&quot;</span>, i2)
                    <span class="hljs-keyword">continue</span> re
                }
        }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">---- <span class="hljs-built_in">continue</span> ---- 
i: 1
i2: 11
i2: 12
i2: 13
i: 2
i2: 11
i2: 12
i2: 13
i: 3
i2: 11
i2: 12
i2: 13
---- <span class="hljs-built_in">continue</span> label ----
i: 1
i2: 11
i: 2
i2: 11
i: 3
i2: 11
</code></pre>
<h3 id="goto-&#x8BED;&#x53E5;">goto &#x8BED;&#x53E5;</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x7684; goto &#x8BED;&#x53E5;&#x53EF;&#x4EE5;&#x65E0;&#x6761;&#x4EF6;&#x5730;&#x8F6C;&#x79FB;&#x5230;&#x8FC7;&#x7A0B;&#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x884C;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;goto &#x8BED;&#x53E5;&#x901A;&#x5E38;&#x4E0E;&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x914D;&#x5408;&#x4F7F;&#x7528;&#x3002;&#x53EF;&#x7528;&#x6765;&#x5B9E;&#x73B0;&#x6761;&#x4EF6;&#x8F6C;&#x79FB;&#xFF0C; &#x6784;&#x6210;&#x5FAA;&#x73AF;&#xFF0C;&#x8DF3;&#x51FA;&#x5FAA;&#x73AF;&#x4F53;&#x7B49;&#x529F;&#x80FD;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x4F46;&#x662F;&#xFF0C;&#x5728;&#x7ED3;&#x6784;&#x5316;&#x7A0B;&#x5E8F;&#x8BBE;&#x8BA1;&#x4E2D;&#x4E00;&#x822C;&#x4E0D;&#x4E3B;&#x5F20;&#x4F7F;&#x7528; goto &#x8BED;&#x53E5;&#xFF0C; &#x4EE5;&#x514D;&#x9020;&#x6210;&#x7A0B;&#x5E8F;&#x6D41;&#x7A0B;&#x7684;&#x6DF7;&#x4E71;&#xFF0C;&#x4F7F;&#x7406;&#x89E3;&#x548C;&#x8C03;&#x8BD5;&#x7A0B;&#x5E8F;&#x90FD;&#x4EA7;&#x751F;&#x56F0;&#x96BE;&#x3002;</p>
<h4 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h4>
<p>goto &#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">goto</span> label
..
.
label: statement
</code></pre>
<p>goto &#x8BED;&#x53E5;&#x6D41;&#x7A0B;&#x56FE;&#x5982;&#x4E0B;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part10/05.png">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;">goto &#x8BED;&#x53E5;&#x8BED;&#x6CD5;&#x6D41;&#x7A0B;&#x56FE;</div>
</center>

<h4 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h4>
<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x5728;&#x53D8;&#x91CF; a &#x7B49;&#x4E8E; 15 &#x7684;&#x65F6;&#x5019;&#x8DF3;&#x8FC7;&#x672C;&#x6B21;&#x5FAA;&#x73AF;&#x5E76;&#x56DE;&#x5230;&#x5FAA;&#x73AF;&#x7684;&#x5F00;&#x59CB;&#x8BED;&#x53E5; LOOP &#x5904;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
   <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
   <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">10</span>

   <span class="hljs-comment">/* &#x5FAA;&#x73AF; */</span>
   LOOP: <span class="hljs-keyword">for</span> a &lt; <span class="hljs-number">20</span> {
      <span class="hljs-keyword">if</span> a == <span class="hljs-number">15</span> {
         <span class="hljs-comment">/* &#x8DF3;&#x8FC7;&#x8FED;&#x4EE3; */</span>
         a = a + <span class="hljs-number">1</span>
         <span class="hljs-keyword">goto</span> LOOP
      }
      fmt.Printf(<span class="hljs-string">&quot;a&#x7684;&#x503C;&#x4E3A; : %d\n&quot;</span>, a)
      a++    
   }  
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a&#x7684;&#x503C;&#x4E3A; : 10
a&#x7684;&#x503C;&#x4E3A; : 11
a&#x7684;&#x503C;&#x4E3A; : 12
a&#x7684;&#x503C;&#x4E3A; : 13
a&#x7684;&#x503C;&#x4E3A; : 14
a&#x7684;&#x503C;&#x4E3A; : 16
a&#x7684;&#x503C;&#x4E3A; : 17
a&#x7684;&#x503C;&#x4E3A; : 18
a&#x7684;&#x503C;&#x4E3A; : 19
</code></pre>
<p>&#x5B9E;&#x4F8B;&#xFF1A;for&#x5FAA;&#x73AF;&#x914D;&#x5408;goto&#x6253;&#x5370;&#x4E5D;&#x4E5D;&#x4E58;&#x6CD5;&#x8868;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    gotoTag()
}

<span class="hljs-keyword">func</span> gotoTag() {
    <span class="hljs-keyword">for</span> i := <span class="hljs-number">1</span>; i &lt; <span class="hljs-number">10</span>; i++ {
        j := <span class="hljs-number">1</span>
        LOOP: <span class="hljs-keyword">if</span> j &lt;= i {
            fmt.Printf(<span class="hljs-string">&quot;%d x %d = %d  &quot;</span>, j, i, j * i)
            j++
            <span class="hljs-keyword">goto</span> LOOP
        } <span class="hljs-keyword">else</span> {
            fmt.Println(<span class="hljs-string">&quot;&quot;</span>)
        }
        j++
    }
}
</code></pre>
<h2 id="&#x65E0;&#x9650;&#x5FAA;&#x73AF;">&#x65E0;&#x9650;&#x5FAA;&#x73AF;</h2>
<p>&#x5982;&#x679C;&#x5FAA;&#x73AF;&#x4E2D;&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x6C38;&#x8FDC;&#x4E0D;&#x4E3A; false &#x5219;&#x4F1A;&#x8FDB;&#x884C;&#x65E0;&#x9650;&#x5FAA;&#x73AF;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; for &#x5FAA;&#x73AF;&#x8BED;&#x53E5;&#x4E2D;&#x53EA;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x6761;&#x4EF6;&#x8868;&#x8FBE;&#x5F0F;&#x6765;&#x6267;&#x884C;&#x65E0;&#x9650;&#x5FAA;&#x73AF;&#xFF1A;</p>
<p>&#x5B9E;&#x4F8B;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">for</span> <span class="hljs-literal">true</span>  {
        fmt.Printf(<span class="hljs-string">&quot;&#x8FD9;&#x662F;&#x65E0;&#x9650;&#x5FAA;&#x73AF;&#x3002;\n&quot;</span>);
    }
}
</code></pre>
<hr>
<h2 id="ps">PS</h2>
<p>&#x7B80;&#x5355;&#x5E94;&#x7528;&#xFF1A;&#x6210;&#x7EE9;&#x67E5;&#x8BE2;&#x7CFB;&#x7EDF;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> (
    <span class="hljs-string">&quot;fmt&quot;</span>
    <span class="hljs-string">&quot;os&quot;</span>
    <span class="hljs-string">&quot;strconv&quot;</span>
)

<span class="hljs-keyword">func</span> main(){
    <span class="hljs-keyword">var</span> score <span class="hljs-keyword">int</span> = <span class="hljs-number">0</span>
    <span class="hljs-keyword">var</span> fenshu <span class="hljs-keyword">string</span> = <span class="hljs-string">&quot;A&quot;</span>
    fmt.Printf(<span class="hljs-string">&quot;&#x6B22;&#x8FCE;&#x8FDB;&#x5165;&#x6210;&#x7EE9;&#x67E5;&#x8BE2;&#x7CFB;&#x7EDF;\n&quot;</span>)
    ZHU: <span class="hljs-keyword">for</span> <span class="hljs-literal">true</span>{
        <span class="hljs-keyword">var</span> choose <span class="hljs-keyword">int</span> = <span class="hljs-number">0</span>
        fmt.Println(<span class="hljs-string">&quot;1.&#x8FDB;&#x5165;&#x6210;&#x7EE9;&#x67E5;&#x8BE2; 2.&#x9000;&#x51FA;&#x7A0B;&#x5E8F;&quot;</span>)
        fmt.Printf(<span class="hljs-string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x5E8F;&#x53F7;&#x9009;&#x62E9;:&quot;</span>)
        fmt.Scanln(&amp;choose)
        fmt.Printf(<span class="hljs-string">&quot;\n&quot;</span>)
        <span class="hljs-keyword">if</span> choose == <span class="hljs-number">1</span>{
            <span class="hljs-keyword">goto</span> CHA
        }
        <span class="hljs-keyword">if</span> choose == <span class="hljs-number">2</span>{
            os.Exit(<span class="hljs-number">1</span>)
        }

    }

    CHA: <span class="hljs-keyword">for</span> <span class="hljs-literal">true</span> {
        fmt.Printf(<span class="hljs-string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x4E00;&#x4E2A;&#x5B66;&#x751F;&#x7684;&#x6210;&#x7EE9;:&quot;</span>)
        fmt.Scanln(&amp;score)
        <span class="hljs-keyword">switch</span> {
            <span class="hljs-keyword">case</span> score &gt;= <span class="hljs-number">90</span>: fenshu = <span class="hljs-string">&quot;A&quot;</span>

            <span class="hljs-keyword">case</span> score &gt;= <span class="hljs-number">80</span> &amp;&amp; score &lt; <span class="hljs-number">90</span>: fenshu = <span class="hljs-string">&quot;B&quot;</span>

            <span class="hljs-keyword">case</span> score &gt;= <span class="hljs-number">60</span> &amp;&amp; score &lt; <span class="hljs-number">80</span>: fenshu = <span class="hljs-string">&quot;C&quot;</span>

            <span class="hljs-keyword">default</span>: fenshu = <span class="hljs-string">&quot;D&quot;</span>
        }

        <span class="hljs-keyword">var</span> c <span class="hljs-keyword">string</span>  = strconv.Itoa(score)
        <span class="hljs-keyword">switch</span>{
            <span class="hljs-keyword">case</span> fenshu == <span class="hljs-string">&quot;A&quot;</span>:
                fmt.Printf(<span class="hljs-string">&quot;&#x4F60;&#x8003;&#x4E86;%s&#x5206;,&#x8BC4;&#x4EF7;&#x4E3A;%s,&#x6210;&#x7EE9;&#x4F18;&#x79C0;\n&quot;</span>, c, fenshu)
            <span class="hljs-keyword">case</span> fenshu == <span class="hljs-string">&quot;B&quot;</span> || fenshu == <span class="hljs-string">&quot;C&quot;</span>:
                fmt.Printf(<span class="hljs-string">&quot;&#x4F60;&#x8003;&#x4E86;%s&#x5206;,&#x8BC4;&#x4EF7;&#x4E3A;%s,&#x6210;&#x7EE9;&#x826F;&#x597D;\n&quot;</span>, c, fenshu)
            <span class="hljs-keyword">case</span> fenshu == <span class="hljs-string">&quot;D&quot;</span>:
                fmt.Printf(<span class="hljs-string">&quot;&#x4F60;&#x8003;&#x4E86;%s&#x5206;,&#x8BC4;&#x4EF7;&#x4E3A;%s,&#x6210;&#x7EE9;&#x4E0D;&#x5408;&#x683C;\n&quot;</span>, c, fenshu)
        }
        fmt.Printf(<span class="hljs-string">&quot;\n&quot;</span>)
        <span class="hljs-keyword">goto</span> ZHU
    }
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part9/" class="navigation navigation-prev " aria-label="Previous page: Go 语言条件语句">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part11/" class="navigation navigation-next " aria-label="Next page: Go 语言函数">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言循环语句","level":"1.11","depth":1,"next":{"title":"Go 语言函数","level":"1.12","depth":1,"path":"part11/README.md","ref":"part11/README.md","articles":[]},"previous":{"title":"Go 语言条件语句","level":"1.10","depth":1,"path":"part9/README.md","ref":"part9/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part10/README.md","mtime":"2021-01-20T02:31:31.357Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

